import { defineStore } from "pinia";
import { store } from "@/store";
import { getResidentInfo, queryResidentInfoByPhone } from "@/api/resident";
import { useUserStoreHook } from "@/store/modules/user";

export type ResidentType = {
  id: number;
  sex: string;
  name: string;
  partId: number;
  phone: string;
  detailAddress: string;
};

export type stateType = {
  residentList: Array<ResidentType>;
  total: number;
};

export const useResidentStore = defineStore({
  id: "pure-Resident",
  state: (): stateType => ({
    residentList: [],
    total: 0
  }),
  actions: {
    async getResidentInfo(currentPage: number, pigeSize: number) {
      await getResidentInfo({
        id: useUserStoreHook().manageId,
        currentPage,
        pigeSize
      }).then(data => {
        this.total = data.total;
        this.residentList = data.list;
      });
    },
    async queryResidentByPhone(
      number: number,
      currentPage: number,
      pigeSize: number
    ) {
      await queryResidentInfoByPhone({
        id: useUserStoreHook().manageId,
        number,
        currentPage,
        pigeSize
      }).then(data => {
        this.total = data.total;
        this.residentList = data.list;
      });
    }
  }
});

export function useResidentStoreHook() {
  return useResidentStore(store);
}
